package com.service.Authorization.service.impl;

import com.service.Authorization.dto.MailBean;
import com.service.Authorization.dto.UserExtendDto;
import com.service.Authorization.entity.ManageInfo;
import com.service.Authorization.entity.PortalUserExtend;
import com.service.Authorization.dao.PortalUserExtendDao;
import com.service.Authorization.service.FeginMstscUnique;
import com.service.Authorization.service.ManageInfoService;
import com.service.Authorization.service.PortalUserExtendService;
import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息扩展(PortalUserExtend)表服务实现类
 *
 * @author makejava
 * @since 2021-08-10 16:14:29
 */
@Service("portalUserExtendService")
public class PortalUserExtendServiceImpl implements PortalUserExtendService {
    @Resource
    private PortalUserExtendDao portalUserExtendDao;
    @Resource
    private ManageInfoService userInfoService;
    @Resource
    FeginMstscUnique feginMstscUnique;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PortalUserExtend queryById(String id) {
        return this.portalUserExtendDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<PortalUserExtend> queryAllByLimit(int offset, int limit) {
        return this.portalUserExtendDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param portalUserExtend 实例对象
     * @return 实例对象
     */
    @Override
    public PortalUserExtend insert(PortalUserExtend portalUserExtend) {
        this.portalUserExtendDao.insert(portalUserExtend);
        return portalUserExtend;
    }

    /**
     * 修改数据
     *
     * @param portalUserExtend 实例对象
     * @return 实例对象
     */
    @Override
    public PortalUserExtend update(PortalUserExtend portalUserExtend) {
        this.portalUserExtendDao.update(portalUserExtend);
        return this.queryById(portalUserExtend.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.portalUserExtendDao.deleteById(id) > 0;
    }
    private UserExtendDto getUserExtendDto(String userId) {
        PortalUserExtend userExtend = portalUserExtendDao.queryByUserId(userId);
        ManageInfo userInfo = userInfoService.queryById(userId);
        if (userInfo == null) {
            return null;
        }
        UserExtendDto userExtendDto = new UserExtendDto();
        if (userExtend != null) {
            userExtendDto = ModelMapperUtil.strictMap(userExtend, UserExtendDto.class);
            if (userExtend.getCity() != null) {
                userExtendDto.setCity(userExtend.getCity().split(","));
            }
            if (userInfo.getTelephone() != null) {
                userExtendDto.setTelephone(userInfo.getTelephone());
            }
            userExtendDto.setUsername(userInfo.getUsername());
            return  userExtendDto;
        }
        userExtendDto.setUsername(userInfo.getUsername());
        if (userInfo.getTelephone() != null) {
            userExtendDto.setTelephone(userInfo.getTelephone());
        }
        return userExtendDto;
    }
    @Override
    public UserExtendDto queryByUserId(String userId) {
        return getUserExtendDto(userId);// 信息是否完善

    }

    @Override
    public void insertUserExtend(UserExtendDto userExtendDto) throws Exception {
        queryByUserIds(userExtendDto);
    }

    @Override
    public void sendMail(MailBean mailBean) {
        redisTemplate.convertAndSend("sendEmail",mailBean); // 发送邮件
    }

    private void queryByUserIds(UserExtendDto userExtend) throws Exception {
        PortalUserExtend userExtend1 = portalUserExtendDao.queryByUserId(userExtend.getUserId());
        PortalUserExtend userExtend2 = ModelMapperUtil.strictMap(userExtend, PortalUserExtend.class);
        if (userExtend1 != null) {
            userExtend2.setId(userExtend1.getId());
            userExtend2 = identicalCode(userExtend, userExtend2);
            portalUserExtendDao.update(userExtend2);
        }else {
            Result result = feginMstscUnique.addApiAccount();
            if (result.getCode() == 200) {
                userExtend2.setId(result.getData().toString());
            }else {
                userExtend2.setId(IdUtil.getStringId());
            }
            userExtend2 = identicalCode(userExtend, userExtend2);
            portalUserExtendDao.insert(userExtend2);
        }
    }
    private PortalUserExtend identicalCode(UserExtendDto userExtend, PortalUserExtend userExtend2) {
        if (userExtend.getTelephone() != null) {
            ManageInfo userInfo = new ManageInfo();
            userInfo.setId(userExtend.getUserId());
            if (userExtend.getTelephone() != null) {
                userInfo.setTelephone(userExtend.getTelephone());
            }
            userInfoService.update(userInfo);
        }
        String[] city = userExtend.getCity();
        userExtend2.setCity(arrayToString(city));
        return userExtend2;
    }

    private String arrayToString(String[] city){
        StringBuilder sb = new StringBuilder();
        if (city != null && city.length > 0) {
            for (int i = 0; i < city.length; i++) {
                if (i < city.length - 1) {
                    sb.append(city[i] + ",");
                } else {
                    sb.append(city[i]);
                }
            }
        }
        return sb.toString();
    }
}
